package com.chenguo.gulimall.product;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import io.prometheus.client.Collector;
import lombok.Data;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @auth cg
 * @data 2021-11-08 09:06
 * @Description
 */
public class testStream {
    @Data
    static   class User {
        private String name;
        //age
        private int age;
        //分数
        private double fraction;

        public User(String name, int age, double fraction) {
            this.name = name;
            this.age = age;
            this.fraction = fraction;
        }

    }

    @Data
    static   class User2 {
        private String name;
        //age
        private int age;

        private int version;
        //分数
        private double fraction;

        public User2(String name, int age, double fraction ,int version) {
            this.name = name;
            this.age = age;
            this.fraction = fraction;
            this.version = version;
        }

    }

    @Test
    public void test1(){
        List<User> userList = new ArrayList<>();
        //向列表的尾部追加指定的元素（可选操作）。
        userList.add(new User("a1", 20, 2.2));
        userList.add(new User("a2", 22, 2.5));
        userList.add(new User("a3", 40, 2.7));
        userList.add(new User("a4", 45, 2.8));
        // 在列表的指定位置插入指定元素（可选操作）。
        userList.add(0,new User("chenguo", 001, 2.8));

        //追加指定 collection 中的所有元素到此列表的结尾
        //userList.addAll(userList);

        //将指定 collection 中的所有元素都插入到列表中的指定位置（可选操作）。
        //userList.addAll(0,userList);

        //从列表中移除所有元素（可选操作）。
        //userList.clear();


        //如果列表包含指定的元素，则返回 true。
        boolean a4 = userList.contains(new User("a4", 45, 2.8));
        System.out.println(a4?"包含该对象":"不包含该对象");

        boolean a41 = userList.equals(new User("a4", 45, 2.8));
        System.out.println(a41?"包含该对象":"不包含该对象");




        userList.forEach(System.out::println);
        System.out.println("==================iterator()==========================");

        int hashCode = 1;
        Iterator i = userList.iterator();
        while (i.hasNext()) {
//            Object obj = i.next();
            User next = (User) i.next();
            System.out.println(next);
//            hashCode = 31*hashCode + (next==null ? 0 : next.hashCode());
            hashCode = (31 * hashCode) + ((next == null) ? 0 : next.getAge());
            System.out.println(hashCode);
        }
        System.out.println("==================E set(int index,E element)=========================");

        User shax = userList.set(0, new User("shax", 18, 45.0));

        userList.forEach(System.out::println);
        System.out.println("==================int size()=========================");
        int size = userList.size();

        System.out.println("size:"+size);

        System.out.println("================== List<E> subList(int fromIndex, int toIndex)  =========================");

        List<User> users = userList.subList(0, 2);//包括fromIndex  不包括toIndex

        users.forEach(System.out::println);
        System.out.println("================== E  get(int index);  =========================");

        User user = userList.get(0);
        User user1 = userList.get(1);
        User user2 = userList.get(2);

        System.out.println("User(0):"+user);
        System.out.println("User(1):"+user1);
        System.out.println("User(2):"+user2);

        System.out.println("================== Object[] toArray()  =========================");

        Object[] objects = userList.toArray();

        for (int j = 0; j < objects.length; j++) {
            System.out.println(" Object[] objects:"+ objects[j]);
        }
        System.out.println("================== stream()  =========================");

        //Stream<User> userStream = userList.stream().filter(t -> t.getAge() > 5);
        List<User2> collect1 = userList.stream()
                .filter(c->{  //过滤
                    return c.getAge()>1;
                })
                .peek(e -> System.out.println("Filtered value: " + e.getName()))
                .map(a -> new User2(a.getName().toUpperCase(Locale.ROOT), a.getAge(), a.getFraction(), 110))

                .peek(e -> System.out.println("Mapped value: " + e))

//                .sorted((menu1,menu2)->{
//                    return (menu1.getAge()==0?0:menu1.getAge()) - (menu2.getAge()==0?0:menu2.getAge());
//                })
                .sorted(Comparator.comparing(User2::getAge).reversed()) //排序
                .limit(2)   //返回由该流的元素组成的流，截断后长度不超过 maxSize
                .skip(1L)  //丢弃流的前 n 个元素后，返回由该流的其余元素组成的流
                .collect(Collectors.toList());


        System.out.println("Collect1::::"+collect1);

        System.out.println("================== peek() 对于并行流管道，可以在上游操作使元素可用的任何时间和线程中调用该操作。如果动作修改共享状态，则负责提供所需的同步 =========================");
        Stream.of("one", "two", "three", "four")
                .filter(e -> e.length() > 3)
                .peek(e -> System.out.println("Filtered value: " + e))
                .map(String::toUpperCase)
                .peek(e -> System.out.println("Mapped value: " + e))
                .collect(Collectors.toList());


        System.out.println("================== peek() 对于并行流管道，可以在上游操作使元素可用的任何时间和线程中调用该操作。如果动作修改共享状态，则负责提供所需的同步 =========================");



    }

    @Test
    public void test01(){

        List<User> userList = new ArrayList<>();
        userList.add(new User("a1", 22, 2.2));
        userList.add(new User("a2", 22, 2.5));
        userList.add(new User("a3", 40, 2.7));
        userList.add(new User("a4", 45, 2.8));

        //返回 对象集合以UsergetAge升序排序:年龄   --默认升序
        System.out.println("================== sorted =========================");
        userList.stream().sorted(Comparator.comparing(User::getAge));

        userList.forEach(System.out::println);

        System.out.println("================== sorted  reversed =========================");

        //返回 对象集合以UsergetAge降序排序  ===这里写在前面 和写在后面要看清楚，弄明白
        userList.stream().sorted(Comparator.comparing(User::getAge).reversed()); //排序结果后再排序,
        userList.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()));//是直接进行排序

        System.out.println("================== limit =========================");
        //返回 对象集合以UsergetAge升序排序:**年龄并返回前n个元素**  --默认升序  ==下面同样写法
        userList.stream().sorted(Comparator.comparing(User::getAge)).limit(2);

        System.out.println("================== skip =========================");

        //返回 对象集合以UsergetAge升序排序:**年龄并去除前 n 个元素**  --默认升序 ==下面同样写法
        userList.stream().sorted(Comparator.comparing(User::getAge)).skip(2);
        System.out.println("================== thenComparing =========================");

        //返回 对象集合以UsergetAge升序 getFraction升序
        userList.stream().sorted(Comparator.comparing(User::getAge).thenComparing(User::getFraction));


        System.out.println("================== reversed().  thenComparing =========================");

        //先以getAge升序,升序结果进行getAge降序,再进行getFraction升序
        userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getFraction));


        System.out.println("================== reverseOrder().  thenComparing =========================");
        //先以getAge降序,再进行getFraction升序
        userList.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()).thenComparing(User::getFraction));

        //先以getAge升序,升序结果进行getAge降序,再进行getFraction降序
        userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getFraction, Comparator.reverseOrder()));

        //先以getAge降序,再进行getFraction降序
        userList.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()).thenComparing(User::getFraction, Comparator.reverseOrder()));


        //先以getAge升序,升序结果进行getAge降序,再进行getFraction升序,结果进行getAge降序getFraction降序
        userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getFraction).reversed());

        //先以getAge升序,再进行getFraction降序
        userList.stream().sorted(Comparator.comparing(User::getAge).thenComparing(User::getFraction, Comparator.reverseOrder()));






    }

    @Test
    public void testUpload() throws FileNotFoundException {

        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI5tS49o2g4fmFRYWMjK8r";
        String accessKeySecret = "rpao99WWo4hm2t1EgTtYDfCS1wRNBy";

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 上传文件流。
        InputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\QQ图片20191123103435.jpg");
        ossClient.putObject("gulimall-cgimage", "QQ图片20191123103435.jpg", inputStream);

// 关闭OSSClient。
        ossClient.shutdown();


    }
}
